'use strict';

const Controller = require('egg').Controller;
const svg = require('svg-captcha');
class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  async captcha(){
    const { ctx } = this;
    let svgData = svg.create({});//text data
    ctx.session.captcha = svgData.text;
    ctx.set('Content-Type','image/svg+xml');
    ctx.body = svgData.data;
  }
  //express cors 中间件 
  async checkCaptcha(){
    const { ctx } = this;
    let {captcha} = ctx.query;
    console.log('request_captcha=',captcha,',ctx.session.captcha=',ctx.session.captcha);
    ctx.body = {
      captcha,
      session_captcha:ctx.session.captcha
    }
  }
}

module.exports = HomeController;
